package com.mz361.stdapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mz361.common.utils.ResultVoUtil;
import com.mz361.common.vo.ResultVo;
import com.mz361.modules.system.domain.InvoiceTitle;
import com.mz361.modules.system.domain.Invoices;
import com.mz361.stdapi.domain.PayOrderResult;
import com.mz361.stdapi.service.InvoiceMangerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author niceale
 * @ date 2024/06/07
 */
@Api(tags = "发票管理接口")
@RestController
@Slf4j
public class InvoiceMangerController {
    @Autowired
    InvoiceMangerService invoiceMangerService;

    @ApiOperation(value = "可开票订单", notes = "用户点击申请开票，调用此接口获取可开票订单")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true)
    })
    @GetMapping("/api/v1/getPayOrders")
    @CrossOrigin
    public ResultVo<List<PayOrderResult>> getPayOrders(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId) {
        log.info("getPayOrders key = {}, timestamp = {}, token = {}, userId = {}", key, timestamp, token, userId);
        List<PayOrderResult> payOrderResults = invoiceMangerService.getPayOrders(userId);
        return ResultVoUtil.success("成功", payOrderResults);
    }

    @ApiOperation(value = "开票申请", notes = "用户选择订单点击开票，填写开票信息调用此接口")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true)
    })
    @PostMapping("/api/v1/applyInvoice")
    @CrossOrigin
    public ResultVo applyInvoice(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId, @RequestBody JSONObject jsonObject) {
        log.info("applyInvoice key = {}, timestamp = {}, token = {}, userId = {}, jsonObject = {}", key, timestamp, token, userId, jsonObject);
        invoiceMangerService.applyInvoice(jsonObject, userId);
        return ResultVoUtil.success("成功");
    }

    @ApiOperation(value = "撤销开票申请", notes = "用户对已有的开票申请撤销调用此接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true),
            @ApiImplicitParam(name = "invoiceId", dataTypeClass = Integer.class, paramType = "query", value = "开票申请ID", required = true),

    })
    @PostMapping("/api/v1/cancelInvoice")
    @CrossOrigin
    public ResultVo cancelInvoice(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId, Long invoiceId) {
        log.info("cancelInvoice key = {}, timestamp = {}, token = {}, userId = {}, invoiceId = {}", key, timestamp, token, userId, invoiceId);
        invoiceMangerService.cancelInvoice(invoiceId, userId);
        return ResultVoUtil.success("成功");
    }

    @ApiOperation(value = "开票记录", notes = "调用此接口获取可开票记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true)
    })
    @GetMapping("/api/v1/getInvoices")
    @CrossOrigin
    public ResultVo<List<Invoices>> getInvoices(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId) {
        log.info("getInvoices key = {}, timestamp = {}, token = {}, userId = {}", key, timestamp, token, userId);
        List<Invoices> invoicesList = invoiceMangerService.getInvoices(userId);
        return ResultVoUtil.success("成功", invoicesList);
    }


    @ApiOperation(value = "发票抬头列表", notes = "产需用户抬头信息")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "用户ID", required = true)
    })
    @GetMapping("/api/v1/getInvoiceTitles")
    @CrossOrigin
    public ResultVo<List<InvoiceTitle>> getInvoiceTitles(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId) {
        log.info("getInvoiceTitles key = {}, timestamp = {}, token = {}, userId = {}", key, timestamp, token, userId);
        List<InvoiceTitle> invoiceTitles = invoiceMangerService.getInvoiceTitles(userId);
        return ResultVoUtil.success("成功", invoiceTitles);
    }

    @ApiOperation(value = "添加抬头", notes = "添加开票抬头信息")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "密钥", required = true),
            @ApiImplicitParam(name = "userId", dataType = "String", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true),
            @ApiImplicitParam(name = "id", dataTypeClass = Integer.class, paramType = "query", value = "抬头ID"),
            @ApiImplicitParam(name = "title", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "抬头", required = true),
            @ApiImplicitParam(name = "titleType", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "抬头类型 0:个人/事业单位，1:企业", required = true),
            @ApiImplicitParam(name = "invoiceType", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "发票类型 0:普票，1:专票", required = true),
            @ApiImplicitParam(name = "taxNumber", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "税号"),
            @ApiImplicitParam(name = "depositBank", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "开户行"),
            @ApiImplicitParam(name = "bankAccount", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "银行账号"),
            @ApiImplicitParam(name = "address", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "地址"),
            @ApiImplicitParam(name = "email", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "邮箱", required = true),
            @ApiImplicitParam(name = "isDefault", dataType = "Boolean", dataTypeClass = Boolean.class, paramType = "query", value = "是否默认", required = true),
            @ApiImplicitParam(name = "remark", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "备注"),
            @ApiImplicitParam(name = "phone", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "电话")
    })
    @PostMapping("/api/v1/addInvoiceTitle")
    @CrossOrigin
    public ResultVo addInvoiceTitle(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId, InvoiceTitle invoiceTitle) {
        log.info("addInvoiceTitle key = {}, timestamp = {}, token = {}, userId = {}, invoiceTitle = {}", key, timestamp, token, userId, JSONObject.toJSONString(invoiceTitle));
        InvoiceTitle invoiceTitleR = invoiceMangerService.saveInvoiceTitle(invoiceTitle, userId);
        if (null != invoiceTitle) {
            return ResultVoUtil.success("成功", invoiceTitleR);
        } else {
            return ResultVoUtil.error("保存失败");
        }
    }

    @ApiOperation(value = "删除抬头", notes = "删除开票抬头信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "string", dataTypeClass = String.class, paramType = "header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "string", dataTypeClass = String.class, paramType = "header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "string", dataTypeClass = String.class, paramType = "header", value = "密钥", required = true),
            @ApiImplicitParam(name = "userId", dataType = "string", dataTypeClass = String.class, paramType = "header", value = "用户ID", required = true),
            @ApiImplicitParam(name = "invoiceTitleId", dataType = "string", dataTypeClass = String.class, paramType = "query", value = "发票抬头ID", required = true)

    })
    @PostMapping("/api/v1/delInvoiceTitle")
    @CrossOrigin
    public ResultVo delInvoiceTitle(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId, String invoiceTitleId) {
        log.info("delInvoiceTitle key = {}, timestamp = {}, token = {}, userId = {}, invoiceTitleId = {}", key, timestamp, token, userId, invoiceTitleId);
        invoiceMangerService.delInvoiceTitle(invoiceTitleId, userId);
        return ResultVoUtil.success("成功");

    }

}
